
 
import java.util.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.file.FileConnection;
 
public class BaigosPedia extends MIDlet implements CommandListener 
{
	private String currDirName;
 
	/*screen commands*/
	private Command search = new Command("Buscar", Command.ITEM, 1);
	private Command view = new Command("Ver", Command.ITEM, 1);
	private Command back = new Command("Volver", Command.BACK, 2);
	private Command exit = new Command("Salir", Command.EXIT, 3);

 
	

	private Image start;
	private Display display;
	private Form form;
	private ScrollCanvas image;
	private SearchScreen searchScreen;
	private TextField searchField;
	private Form searchForm;

 
	public BaigosPedia() 
	{
		searchForm = new Form("Buscar");
		searchField = new TextField("Buscar", "", 20, TextField.ANY);
		searchForm.append(searchField);
		//search = new Command("Buscar", Command.SCREEN, 1);
		searchForm.addCommand(search);
		searchForm.addCommand(exit);
		searchForm.setCommandListener(this);

		//open index file
		//fconn = (FileConnection) Connector.open("file:///");



	}
 
	public void startApp() 
	{


        boolean isAPIAvailable = false;
        if (System.getProperty("microedition.io.file.FileConnection.version") != null)
        {
            isAPIAvailable = true;

            System.out.println("iniciando BaigosPedia");
            try 
            {
                display = Display.getDisplay(this);
		display.setCurrent(searchForm);


            } 
            catch (SecurityException e) 
            {
                System.out.println(e);
            } 
            catch (Exception e) {System.out.println(e);}
        }
        else
        {
            StringBuffer splashText = new StringBuffer
            (getAppProperty("MIDlet-Name")).append("\n").append(
            getAppProperty("MIDlet-Vendor")).
            append(isAPIAvailable?"":"\nFileConnection API not available");
            Alert splashScreen = new Alert(null,splashText.toString(),
            null,AlertType.INFO);
            splashScreen.setTimeout(3000);
            Display.getDisplay(this).setCurrent(splashScreen);
        }
    
	}
 
	public void pauseApp() {}
 

	public void destroyApp(boolean cond) 
	{
	        notifyDestroyed();
	}
 
	public void commandAction(Command c, Displayable d) 
	{

        if (c == view) 
        {
            
            
            new Thread(new Runnable() 
            {
                public void run() 
                {
                    
                        showArticle(17);
                    
			
                }
            }                       ).start();

        }
	else if (c == search) 
        {
	System.out.println("Buscando..."+searchField.getString());
	searchArticle(searchField.getString());
	//searchScreen = new SearchScreen();
	//display = Display.getDisplay(this);
	//display.setCurrent(searchScreen); 

        }         
	else if (c == back) 
        {


        } 
        else if (c == exit) 
        {
            destroyApp(false);
        }
	}

    
	/*shows an article*/
public void showArticle(int articleid) 
{

	new Thread(new Runnable() 
            {
                public void run() 
                {
                    

	
		try 
		{
			FileConnection fileConnection = (FileConnection)Connector.open("file:///root1/articles.txt",Connector.READ );                                    
			InputStream inputStream = fileConnection.openDataInputStream(); 
			//byte[] indexData = new byte[(int)fileConnection.fileSize()];
			byte[] indexData = new byte[8];
			inputStream.read(indexData,0,indexData.length);
			String s = new String(indexData);
			System.out.println(s);			

		}
		catch (Exception ex) 
		{
			System.err.println("Error open file connection."+ex.toString());               
		}

	
                    
			
                }
            }                       ).start();


        
		//display = Display.getDisplay(this);


		//backfromimage = new Command("Back", Command.BACK, 1);
		//rotate = new Command("Rotate", Command.SCREEN, 1);
		//zoom = new Command("Zoom", Command.SCREEN, 2);


		//image = new ScrollCanvas();

		//browser.removeCommand(exit);
		//image.addCommand(zoom);
		//image.addCommand(rotate);
		//image.addCommand(backfromimage);
		//image.setCommandListener(this);
		//display.setCurrent(image); 
}

	/*search article in the index*/
	public int searchArticle(String keyword)
	{

	new Thread(new Runnable() 
            {
                public void run() 
                {
                    

	
		try 
		{
			FileConnection fileConnection = (FileConnection)Connector.open("file:///root1/articles.txt",Connector.READ );                                    
			InputStream inputStream = fileConnection.openDataInputStream(); 
			//byte[] indexData = new byte[(int)fileConnection.fileSize()];
			byte[] indexData = new byte[512];
			int time1=(int)System.currentTimeMillis();

			//for(int i=0;i<10000;i=i+8){
			while(inputStream.read(indexData,0,512)>0){
			//System.out.println(i);	
			//byte[] indexData = new byte[8];
			
			//String s = new String(indexData);
			//System.out.println(s);	
			}
			int time2=(int)System.currentTimeMillis()-time1;
			System.out.println(time2);

		}
		catch (Exception ex) 
		{
			System.err.println("Error open file connection."+ex.toString());               
		}
		catch (java.lang.OutOfMemoryError e) {
	            throw e;
		}

	
                    
			
                }
            }                       ).start();

	return 0;

	}


} 
 
 

